<template>
  <div class="app-container">
    <el-card class="query-card">
    <el-row :gutter="20">
      <el-col :span="4">
          <el-input
            v-model="filterText"
            placeholder="输入关键字进行过滤"
          />
          <el-tree
            ref="tree"
            :data="deptTreeData"
            style="margin-top: 10px"
            default-expand-all
            show-checkbox
            :props="{children: 'children',label: 'deptName'}"
            :filter-node-method="filterNode"
            node-key="id"
            check-strictly
            @check-change="deptTreeCheck"
          />
<!--        </el-card>-->
      </el-col>
      <el-col :span="20">
<!--        <el-card class="query-card">-->
          <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
            <el-form-item prop="username" label="用户名">
              <el-input v-model="page.query.username" clearable placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item prop="phoneNum" label="手机号">
              <el-input v-model="page.query.phoneNum" clearable placeholder="请输入手机号" />
            </el-form-item>
            <el-form-item prop="state" label="状态">
              <dict-select v-model="page.query.state" dict-key="comm_state" placeholder="请选择状态" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="el-icon-search" @click="handleSearch">查询</el-button>
              <el-button class="el-icon-refresh" @click="resetQueryForm">重置</el-button>
            </el-form-item>
          </el-form>
<!--        </el-card>
        <el-card class="table-card">-->
          <!--      <div>
                  <span>用户信息列表</span>
                </div>
                <el-divider />-->
          <div class="table-button">
            <el-button type="primary" class="el-icon-plus" @click="saveClick">新增
            </el-button>
            <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
            <el-tooltip content="刷新" class="table-right-btn">
              <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
            </el-tooltip>
          </div>
          <el-table
            v-loading="tableLoading"
            :data="tableData"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="45" />
            <el-table-column label="用户名" prop="username" min-width="100" />
            <el-table-column label="手机号" prop="phoneNum" min-width="120" />
            <el-table-column label="邮箱" prop="email" min-width="160" />
            <el-table-column label="角色" prop="roleNames" min-width="120" />
            <el-table-column label="部门" prop="deptName" min-width="140" />
            <el-table-column label="状态" prop="state" min-width="80">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.state"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-value="1"
                  inactive-value="0"
                  @change="openOrClose(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="160">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  @click="resetPwdClick(scope.row)"
                >重置密码
                </el-button>
                <el-button
                  type="text"
                  @click="updateClick(scope.row)"
                >修改
                </el-button>
                <el-popconfirm
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  placement="top"
                  style="margin-left: 10px"
                  title="是否删除此条数据"
                  @confirm="handleDelConfirm(scope.row)"
                >
                  <el-button slot="reference" type="text">删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :page-sizes="[10, 20, 30, 40]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
      </el-col>
    </el-row>
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { deleteUser, resetPassword, selectUserList, updateUser } from '@/api/system/user'
import { selectDeptTree } from '@/api/system/dept'
import SubmitModule from '@/views/system/user/modules/submitModule.vue'

export default {
  name: 'Index',
  components: { SubmitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      dialogVisible: false,
      selected: [],
      dialogTitle: '',
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          username: '',
          email: '',
          deptId: '',
          state: ''
        }
      },
      deptTreeData: [],
      filterText: ''
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  // 初始化方法
  mounted() {
    this.doPage()
    this.getDeptList()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    // 分页查询
    doPage() {
      this.tableLoading = true
      selectUserList(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query = {}
      this.doPage()
    },
    // 批量删除
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        deleteUser({ ids: this.selected.map(e => e.id) }).then(resp => {
          if (resp.success) {
            this.$message.success('操作成功')
            this.doPage()
          }
        }).finally(() => {
          this.tableLoading = false
        })
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否删除此用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = this.selected[0]
        if (row) {
          param = row
        }
        deleteUser(param.id).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.doPage()
        })
      })
    },
    openOrClose(row) {
      this.tableLoading = true
      updateUser(row).then(resp => {
        if (resp.success) {
          this.$message.success('操作成功')
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 新增按钮点击
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init()
      })
    },
    // 修改按钮点击
    updateClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(row.id)
      })
    },
    // 重置密码点击
    resetPwdClick(row) {
      this.$confirm('是否重置此用户的密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        resetPassword(row.id).then(resp => {
          if (resp.success) {
            this.$message.success('重置成功')
          }
        }).finally(() => {
          this.doPage()
        })
      })
    },
    // 获取机构
    getDeptList() {
      this.deptTreeVisible = false
      selectDeptTree({}).then(resp => {
        if (resp.success) {
          this.deptTreeData = resp.data
        }
      })
    },
    // 机构树点击
    deptTreeCheck(node, check, childCheck) {
      this.$refs.tree.setCheckedKeys([]) // 删除所有选中节点
      this.page.query.deptId = ''
      if (check) {
        this.page.query.deptId = node.id
        this.$refs.tree.setCheckedNodes([node]) // 选中已选中节点
      }
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.dialogVisible = false
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteUser(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.deptName.indexOf(value) !== -1
    }
  }
}
</script>

<style scoped>

</style>
